package com.mars.base.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.thoughtworks.xstream.XStream;

public class XmlUtils {

	/**
	 * 对象转XML
	 * @param obj 对象
	 * @param clazz 对象类型
	 * @param alias 根标签名称
	 * @return
	 */
	public static String beanToXml(Object obj, String alias) {
		XStream xStream = new XStream();
		if(null != alias) {
			xStream.alias(alias, obj.getClass());
		} else {
			xStream.processAnnotations(obj.getClass());
		}
		return xStream.toXML(obj) ;
	}
	public static String beanToXml(Object obj) {
		return beanToXml(obj, null) ;
	}
	
	/**
	 * XML转对象
	 * @param xml 字符串
	 * @param clazz 对象类型
	 * @param alias 根标签名称
	 * @return
	 */
	public static Object xmlToBean(String xml, Class<?> clazz, String alias) {
		XStream xStream = new XStream();
		if(null != clazz && null != alias) {
			xStream.alias(alias, clazz);
		}
		return xStream.fromXML(xml) ;
	}
	public static Object xmlToBean(String xml) {
		return xmlToBean(xml, null, null) ;
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String, String> parseXml(InputStream inputStream) {
		// 将解析结果存储在HashMap中
		Map<String, String> map = null ;
		try {
			map = new HashMap<String, String>();

			// 读取输入流
			SAXReader reader = new SAXReader();
			Document document = reader.read(inputStream);
			// 得到xml根元素
			Element root = document.getRootElement();
			// 得到根元素的所有子节点
			List<Element> elementList = root.elements();

			// 遍历所有子节点
			for (Element e : elementList) {
				map.put(e.getName(), e.getText());
			}
			
		} catch (DocumentException e) {
			e.printStackTrace();
		} finally {
			// 释放资源
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return map;
	}
	
	
	public static void main(String[] args) {
		
		String str = "<xml>"+
					"<id>1</id>"+
					"<name>张三</name>"+
					"<datetime>2016-12-23 12:58:05.870 UTC</datetime>"+
					"</xml>";
		
		Person xmlToBean = (Person) XmlUtils.xmlToBean(str, Person.class, "xml") ;
		
		System.out.println(xmlToBean.getName());
		
		String beanToXml = XmlUtils.beanToXml(new Person(1, "123", new Date()), "xml") ;
		System.out.println(beanToXml);
		
		List<Person> list = new ArrayList<Person>() ;
		list.add(new Person(1, "张三", new Date())) ;
		list.add(new Person(2, "李四", new Date())) ;
		
		String beanToXml2 = XmlUtils.beanToXml(list, "person") ;
		System.out.println(beanToXml2);
		
		Map<String, Person> map = new HashMap<String, Person>();
		map.put("p1", new Person(1, "张三", new Date())) ;
		map.put("p2", new Person(2, "李四", new Date())) ;
		map.put("p3", new Person(3, "王八", new Date())) ;
		
		String mapc = XmlUtils.beanToXml(map, "person") ;
		System.out.println(mapc);
		
		Person person = new Person(1, "张三", new Date()) ;
		person.setPerson(list);
		
		String pp = XmlUtils.beanToXml(person, "person") ;
		System.out.println(pp);
		
	}
	
	
}

class Person {
	
	private Integer id ;
	
	private String name ;
	
	private Date datetime ;
	
	private List<Person> person ;

	public List<Person> getPerson() {
		return person;
	}

	public void setPerson(List<Person> person) {
		this.person = person;
	}

	public Person(Integer id, String name, Date datetime) {
		super();
		this.id = id;
		this.name = name;
		this.datetime = datetime;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getDatetime() {
		return datetime;
	}

	public void setDatetime(Date datetime) {
		this.datetime = datetime;
	}
	
	
	
}